<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class Courses extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('Courses/CoursesModel','courses');
	}

	//列表页
	public function index()
	{
		//获取场馆
		$this->load->model('User/CoachModel','coach');
		$data['venue'] = $this->coach->get_venue_list();

		//获取教练
		$coach_list = $this->coach->get_list_data();
		$data['coach_list'] = $coach_list['list'];

		$this->load->view('courses_list', $data);
	}


	//获取列表数据
	public function get_list()
	{

		$page = $this->input->get('page');
		$page = $page ? $page : 1;

		$term['venue'] = $this->input->get('venue');
		$term['keyword'] = $this->input->get('keyword');
		$term['coach'] = $this->input->get('coach');

		$res = $this->courses->get_courses_list($term,$page);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();
	}


	//添加、编辑页
	public function courses_info()
	{
		$data['courses_id'] = intval($this->uri->segment(3));

		$this->load->model('User/CoachModel','coach');
		$data['venue'] = $this->coach->get_venue_list();

		//获取教练
		$coach_list = $this->coach->get_list_data();
		$data['coach_list'] = $coach_list['list'];

		//获取动作
		$query= $this->db->query("SELECT id, name FROM nmp_sport");
		$data['sport'] = $query->result_array();

		//编辑
		if ($data['courses_id'] > 0) 
		{
			$res = $this->courses->get_courses_row($data['courses_id']);
			$sport = json_encode(explode(',',$res['sport']));
			$suitable = @implode('|', json_decode($res['suitable']));
			$tool = json_decode($res['tool'],TRUE);
		}

		$data['name'] = isset($res['name']) ? $res['name'] : '';
		$data['theme_img'] = isset($res['theme_img']) ? $res['theme_img'] : '';
		$data['title'] = isset($res['title']) ? $res['title'] : '';
		$data['shop_id'] = isset($res['shop_id']) ? $res['shop_id'] : '';
		$data['cid'] = isset($res['cid']) ? $res['cid'] : '';
		$data['nandu'] = isset($res['nandu']) ? $res['nandu'] : '';
		$data['ranzhi'] = isset($res['ranzhi']) ? $res['ranzhi'] : '';
		$data['duration'] = isset($res['duration']) ? $res['duration'] : '';
		$data['info'] = isset($res['info']) ? $res['info'] : '';
		$data['status'] = isset($res['status']) ? $res['status'] : '0';
		$data['content'] = isset($res['content']) ? $res['content'] : '';
		$data['type_id'] = isset($res['type_id']) ? $res['type_id'] : '';
		$data['suitable'] = isset($suitable) ? $suitable : '';
		$data['edit_sport'] = isset($sport) ? $sport : '[]';
		$data['apellation'] = isset($tool['title']) ? $tool['title'] : '';
		$data['desc'] = isset($tool['info']) ? $tool['info'] : '';
		$data['max_num'] = isset($res['max_num']) ? $res['max_num'] : '';

		//图片上传地址
		$data['image_upload_url'] = $this->image_load_url;

		$this->load->view('courses_info', $data);
	}


	//提交入库
	public function insert_courses()
	{
		$field = array('theme_img','type_id','name','title','shop_id','cid','nandu','ranzhi','duration','info','content','content','suitable','max_num');
		$arr = $this->input->post($field, TRUE);

		$apellation = $this->input->post('apellation', TRUE);
		$desc = $this->input->post('desc', TRUE);

		$arr['sport'] = !empty($_POST['sport']) ? join(',',$_POST['sport']) : '';
		
		//处理数据
		$sui_arr = explode('|',$arr['suitable']);
		$suitable = array();
		for ($i=0; $i < count($sui_arr); $i++) { 
			array_push($suitable, $sui_arr[$i]);
		}
		$arr['suitable'] = json_encode($suitable);
		$tool = array('title'=>$apellation,'info'=>$desc);
		$arr['tool'] = json_encode($tool);

		$course_id = $this->input->post('course_id');

		//添加
		if (empty($course_id)) 
		{
			$res = $this->courses->inser_course_data($arr);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '添加成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '添加失败！';
			}
		}
		//更新
		else
		{
			$res = $this->courses->updata_course_data($arr,$course_id);

			if ($res) 
			{
				$data['status'] = 200;
				$data['message'] = '更新成功！';

			}
			else
			{
				$data['status'] = 400;
				$data['message'] = '更新失败！';
			}

		}

		echo json_encode($data);exit();

	}


	//删除
	public function delete_course(){
		
		$course_id = $this->input->get('id', true);

		//$res = $this->courses->remove_course($course_id);
		
		if($this->courses->is_have_appoint($course_id)>0){//是否还有预约
			$data['status'] = 400;
			$data['message'] = '删除失败！该课程还有预约未上完';
			echo json_encode($data);exit();
		}
		
		
		$this->courses->del("id='{$course_id}'");
		$this->load->model('Courses/CourseCardModel','CourseCardModel');
		$this->CourseCardModel->del("course_id='{$course_id}'");
		$res = 1;
		//$res = $this->courses->updata_course_data(array("status"=>0),$course_id);
		
		if ($res){
			$data['status'] = 200;
			$data['message'] = '删除成功！';
		}
		else{
			$data['status'] = 400;
			$data['message'] = '删除失败！';
		}

		echo json_encode($data);exit();

	}




}



?>